package cn.iocoder.yudao.module.biz.service.risk.controlMeasures;

import cn.iocoder.yudao.module.biz.controller.admin.risk.controlMeasures.vo.*;
import cn.iocoder.yudao.module.biz.convert.risk.controlMeasures.BizControlMeasuresConvert;
import cn.iocoder.yudao.module.biz.dal.dataobject.risk.controlMeasures.BizControlMeasuresDO;
import cn.iocoder.yudao.module.biz.dal.mysql.risk.controlMeasures.BizControlMeasuresMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.biz.enums.ErrorCodeConstants.*;

/**
 * 管控措施 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class BizControlMeasuresServiceImpl implements BizControlMeasuresService {

    @Resource
    private BizControlMeasuresMapper controlMeasuresMapper;

    @Override
    public String createControlMeasures(BizControlMeasuresCreateReqVO createReqVO) {
        // 插入
        BizControlMeasuresDO controlMeasures = BizControlMeasuresConvert.INSTANCE.convert(createReqVO);
        controlMeasuresMapper.insert(controlMeasures);
        // 返回
        return controlMeasures.getId();
    }

    @Override
    public void updateControlMeasures(BizControlMeasuresUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateControlMeasuresExists(updateReqVO.getId());
        // 更新
        BizControlMeasuresDO updateObj = BizControlMeasuresConvert.INSTANCE.convert(updateReqVO);
        controlMeasuresMapper.updateById(updateObj);
    }

    @Override
    public void deleteControlMeasures(String id) {
        // 校验存在
        this.validateControlMeasuresExists(id);
        // 删除
        controlMeasuresMapper.deleteById(id);
    }

    private void validateControlMeasuresExists(String id) {
        if (controlMeasuresMapper.selectById(id) == null) {
            throw exception(CONTROL_MEASURES_NOT_EXISTS);
        }
    }

    @Override
    public BizControlMeasuresDO getControlMeasures(String id) {
        return controlMeasuresMapper.selectById(id);
    }

    @Override
    public List<BizControlMeasuresDO> getControlMeasuresList(Collection<String> ids) {
        return controlMeasuresMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BizControlMeasuresDO> getControlMeasuresPage(BizControlMeasuresPageReqVO pageReqVO) {
        return controlMeasuresMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BizControlMeasuresDO> getControlMeasuresList(BizControlMeasuresExportReqVO exportReqVO) {
        return controlMeasuresMapper.selectList(exportReqVO);
    }

    @Override
    public List<BizControlMeasuresRespVO> getControlMeasuresListByHazardId(String hazardSourceId) {
        return controlMeasuresMapper.getControlMeasuresListByHazardId(hazardSourceId);
    }
}
